﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Next.CodeGen.Core.Utility
{
    public class XMLSchemaUtility
    {
        public static List<Table> XMLToList()
        {
            List<Table> Tables = new List<Table>();
            Table CurrentTable = new Table();

            //using (XmlTextReader XMLSchema = new XmlTextReader(new StringReader(Schema)))
            using (XmlTextReader XMLSchema = new XmlTextReader(@Properties.Settings.Default.CommonControlsProjectPath + "\\Schema\\Schema.xml"))
            {
                bool FirstTable = true;
                while (XMLSchema.Read())
                {
                    if (XMLSchema.NodeType == XmlNodeType.Element)
                    {
                        switch (XMLSchema.Name)
                        {
                            case "Table":
                                #region case "Table":
                                //COND : First Tables
                                if (FirstTable)
                                {
                                    FirstTable = false;
                                    CurrentTable.Name = XMLSchema.GetAttribute("name");
                                }
                                //COND : New table detected
                                if (CurrentTable.Name != XMLSchema.GetAttribute("name") && CurrentTable.Name != "" && !FirstTable)
                                {
                                    Tables.Add(CurrentTable);
                                    CurrentTable = new Table();
                                    CurrentTable.Name = XMLSchema.GetAttribute("name");
                                }
                                break;
                                #endregion
                            case "Column":
                                #region case "Column":
                                Column CurrentColumn = new Column();
                                CurrentColumn.Name = XMLSchema.GetAttribute("name");
                                CurrentColumn.Type = XMLSchema.GetAttribute("type");
                                CurrentColumn.IsIdentity = XMLSchema.GetAttribute("is_identity");
                                CurrentColumn.IsRequired = XMLSchema.GetAttribute("is_required");
                                CurrentTable.Columns.Add(CurrentColumn);
                                break;
                                #endregion
                            case "Relation":
                                #region case "Relation":
                                Relation CurrentRelation = new Relation();
                                CurrentRelation.LocalTable = XMLSchema.GetAttribute("localtable");
                                CurrentRelation.LocalColumn = XMLSchema.GetAttribute("localcolumn");
                                CurrentRelation.ReferenceTable = XMLSchema.GetAttribute("referencetable");
                                CurrentRelation.ReferenceColumn = XMLSchema.GetAttribute("referencecolumn");
                                CurrentRelation.DeleteAction = XMLSchema.GetAttribute("deleteaction");
                                CurrentRelation.UpdateAction = XMLSchema.GetAttribute("updateaction");
                                CurrentTable.Relations.Add(CurrentRelation);
                                break;
                                #endregion
                        }
                    }
                }
                Tables.Add(CurrentTable);
            }
            return Tables;
        }

        public static string ListToXML()
        {
            return null;
        }
    }
}
